package com.wzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.domain.ResponseResult;
import com.wzy.domain.entity.Category;
import com.wzy.domain.vo.CategoryListVO;
import com.wzy.mapper.CategoryMapper;
import com.wzy.service.CategoryService;
import com.wzy.service.UserCommonService;
import com.wzy.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.wzy.constant.SystemConstants.NORMAL_STATUS;

/**
 * 分类业务层
 */
@Service("CategoryServiceImpl")
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService
{
    private final CategoryMapper categoryMapper;
    private final HttpServletRequest httpServletRequest;
    private final UserCommonService userCommonService;

    /**
     * 获取分类列表
     */
    @Override
    public ResponseResult<List<CategoryListVO>> getCategoryList()
    {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        // 是正常状态的分类
        queryWrapper.eq(Category::getCategoryStatus, NORMAL_STATUS);
        List<Category> categories = categoryMapper.selectList(queryWrapper);

        List<CategoryListVO> categoryListVOS = BeanCopyUtils.copyBeanList(categories, CategoryListVO.class);
        return ResponseResult.okResult(categoryListVOS);
    }

    /**
     * 新建文章分类
     */
    @Override
    public ResponseResult<Void> addCategory(CategoryListVO category)
    {
        userCommonService.checkRole(httpServletRequest);
        Category category1 = BeanCopyUtils.copyBean(category, Category.class);
        save(category1);
        return ResponseResult.okResult();
    }
}




